package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/20 19:05
 */
/*
* 套餐管理
* */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /*
    * 新增套餐
    * */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @PostMapping
    public Result insert(@RequestBody SetmealDTO setmealDTO){
        //权限管理  不是管理员无法修改
        Long currentId = BaseContext.getCurrentId();
        if (currentId!=1){
            return Result.error("没有权限，仅管理员可修改");
        }
        ///////////////////////////////////////////////////
        setmealService.insert(setmealDTO);
        return Result.success();}
    /*
    * 分页查询套餐
    * */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.pageQuary(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /*
    * 批量删除套餐
    * @RequestParam注解将ids字符串拆分为单独的数
    * */

    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        //权限管理  不是管理员无法修改
        Long currentId = BaseContext.getCurrentId();
        if (currentId!=1){
            return Result.error("没有权限，仅管理员可修改");
        }
        ///////////////////////////////////////////////////
        setmealService.delete(ids);
        return Result.success();
    }
    /*
    * 根据id查询套餐 --》回显信息
    * */
    @GetMapping("/{id}")
    public Result<SetmealVO> getBySetmealId(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getBySetmealId(id);
        return Result.success(setmealVO);
    }
    /*
    * 修改套餐
    * */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    public Result update(@RequestBody SetmealVO setmealVO){
        //权限管理  不是管理员无法修改
        Long currentId = BaseContext.getCurrentId();
        if (currentId!=1){
            return Result.error("没有权限，仅管理员可修改");
        }
        ///////////////////////////////////////////////////
        setmealService.update(setmealVO);
        return Result.success();
    }
    /*
    * 启售停售套餐
    * */

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result changeStatu(@PathVariable Integer status,Long id){
       setmealService.changeStatu(status,id);
       return Result.success();
   }
}
